package org.kzwl.book.controller.web;

import cn.hutool.core.util.IdcardUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.kzwl.book.controller.filter.CookieUtil;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.advice.Consts;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.UseRegisterBO;
import org.kzwl.comm.pojo.bo.WebUserLoginBO;
import org.kzwl.comm.pojo.bo.user.UpdateUserBO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.app.User;
import org.kzwl.service.app.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/webUser")
@Api("PC用户业务")
public class WebUserController {

    @Autowired
    UserService userService;

    @ApiOperation("PC用户注册")
    @PostMapping("/useRegister")
    public ResponseData<Void> useRegister(HttpServletRequest request,@RequestBody @Valid UseRegisterBO useRegisterBO)
    {

        if(useRegisterBO.getCuVerificationCode() == null || "".equals(useRegisterBO.getCuVerificationCode()))
        {
            throw new BizException("验证码为空");
        }

        String sessionAttribute = (String) request.getSession().getAttribute(Consts.SessionData.IMAGESID);

        if ( !sessionAttribute.equals(useRegisterBO.getCuVerificationCode()))
        {
            throw new BizException("验证码错误或已过期");
        }
        return userService.useRegister(useRegisterBO);
    }

    @ApiOperation(value = "PC用户登录")
    @PostMapping("/webUserLogin")
    public ResponseData<String> webUserLogin(HttpSession session, @RequestBody @Valid WebUserLoginBO webUserLoginBO, HttpServletResponse response)
    {
        String sessionAttribute = (String) session.getAttribute(Consts.SessionData.IMAGESID);
        if (!sessionAttribute.equals(webUserLoginBO.getCuVerificationCode()))
        {
            throw new BizException("验证码错误或已过期");
        }

        User user = userService.WebLogin(webUserLoginBO);
        String jwt = MyUtils.createJwt(JSON.toJSONString(user));
        session.setAttribute(Consts.SessionData.SESSIONID,jwt);
        Cookie cookie = new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(30);
        response.addCookie(cookie);
        return new ResponseData<>(jwt);
    }

    @PostMapping("/queryToken")
    public ResponseData<String> queryToken(HttpSession session)
    {
        String sessionAttribute = (String) session.getAttribute(Consts.SessionData.SESSIONID);

        return new ResponseData<>(sessionAttribute);
    }
    @PostMapping("/clearToken")
    public ResponseData<Void> clearToken(HttpSession session)
    {
        session.invalidate();
        return new ResponseData<>();
    }

    @ApiOperation(value = "PC用户修改个人信息")
    @PostMapping("/updateUser")
    public ResponseData<Void> updateUser(@RequestBody @Valid UpdateUserBO bo){
        userService.updateUser(bo);
        return new ResponseData<>();
    }


    @PostMapping("/userCount")
    public ResponseData<Integer> userCount(){
        Integer i = userService.userCount();
        return new ResponseData<>(i);
    }



}
